<template>
    <div>
        <!-- <el-form ref="editForm" :model="formFileds" label-position="left" label-width="auto">
            <el-row :gutter="24">
                <el-col :span="5">
                    <el-form-item label="方式：" prop="sjd">
                        <el-select v-model="formFileds.sjd" clearable @change="selectsjd">
                            <el-option label="按日" value="day"></el-option>
                            <el-option label="按月" value="month"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="5">
                    <el-form-item label="时间：" prop="ptime">
                        <el-input v-model="formFileds.ptime" placeholder="请输入" clearable></el-input>
                    </el-form-item>
                </el-col> -->
        <!-- <el-col :span="5">
                    <el-form-item label="收款状态" prop="pskzt">
                        <el-select v-model="formFileds.pskzt" clearable>
                            <el-option v-for="item in skztOptions" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="5">
                    <el-form-item label="是否开票" prop="pkpzt">
                        <el-select v-model="formFileds.pkpzt" clearable>
                            <el-option label="已开发票" value="已开发票"></el-option>
                            <el-option label="未开发票" value="未开发票"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="24">
                <el-col :span="5">
                    <el-form-item label="评估师" prop="ppgs">
                        <el-input v-model="formFileds.ppgs" placeholder="请输入" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="5">
                    <el-form-item label="审核员" prop="pshy">
                        <el-input v-model="formFileds.pshy" placeholder="请输入" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="5">
                    <el-form-item label="对接人" prop="pdjr">
                        <el-input v-model="formFileds.pdjr" placeholder="请输入" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="5">
                    <el-form-item label="业务类型" prop="pywlx">
                        <el-select v-model="formFileds.pywlx" clearable>
                            <el-option v-for="item in ywlxOptions" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col> -->
        <!-- <el-col :span="4"> -->
        <!-- <el-form-item>
                    <el-button type="primary" @click="search()"> 查询 </el-button>
                    <el-button type="primary" @click="download()"> 导出 </el-button>
                </el-form-item> -->
        <!-- </el-col> -->
        <!-- </el-row>
        </el-form> -->
        <p>时间<i style="width:14px;display: inline-block;"></i>
            <el-input type="text" v-model="ptime" placeholder="请输入   例：2022 / 202201" style="width:16%"
                @keyup.enter.native="search()"></el-input>
            <el-button type="primary" @click="search()">查询</el-button>
            <el-button type="primary" @click="download()"> 导出 </el-button>
        </p>
        <el-table :data="tableData" style="margin-top: 22px; width: 100%" border
            :header-cell-style="{ textAlign: 'center' }" :cell-style="{ textAlign: 'center' }"
            :summary-method="getSummaries" show-summary>
            <el-table-column prop="date" label="时间"></el-table-column>
            <el-table-column prop="ywsl" label="案件数量"></el-table-column>
            <el-table-column prop="ssje" label="实收金额"></el-table-column>
            <el-table-column prop="ysje" label="应收金额"></el-table-column>
            <el-table-column prop="pjajdj" label="平均案件单价"></el-table-column>
            <el-table-column prop="ajcb" label="案件成本"></el-table-column>
            <el-table-column prop="ajml" label="案件毛利"></el-table-column>
            <el-table-column prop="ajmll" label="案件毛利率"></el-table-column>

        </el-table>
    </div>
</template>
       
<script>
// import { selectAll, find, sort } from "@/api/evaluation/financeList";
import { findByName } from '@/api/systemManage/dataDictionary';
import { getyear, getmonth, downday } from '@/api/financialManagement/financialStatistics';
export default {
    name: "Table",
    data () {
        return {
            skztOptions: [],
            ywlxOptions: [],
            // text: "",
            // formFileds: {
            //     psjd: '',
            //     pskzt: '',
            //     pkpzt: '',
            //     pywlx: '',
            //     selectday: '',
            //     selectmonth: '',
            //     pdjr: '',
            //     ppgs: '',
            //     pshy: '',
            //     oysje: '',
            //     ssje: '',
            //     ocount: ''
            // },
            ptime: '',
            tableData: [],
            downdate: '',
        }
    },
    methods: {

        getdataDictionary () {
            findByName('input=' + '业务类型').then((res) => {
                for (var i = 0; i < res.data.length; i++) {
                    if(res.data[i].dictName=='业务类型')
                    this.ywlxOptions.push({
                        label: res.data[i].dictValue,
                        value: res.data[i].dictValue
                    })
                }
            })
            findByName('input=' + '收款状态').then((res) => {
                for (var i = 0; i < res.data.length; i++) {
                    if(res.data[i].dictName=='收款状态')
                    this.skztOptions.push({
                        label: res.data[i].dictValue,
                        value: res.data[i].dictValue
                    })
                }
            })
        },
        // selectsjd () {
        //     if (this.formFileds.sjd == '月') {
        //         this.tableData = this.monthableData
        //     } else {
        //         this.tableData = this.daytableData
        //     }
        // },
        download () {
            window.location.href = "/api/cwtj/downday?ptime=" + this.downdate
        },
        gettest () {
            var nowDate = new Date()
            var year = nowDate.getFullYear()
            year = year.toString()
            this.downdate = year
            getyear({ ptime: year }).then((res) => {
                this.tableData = res.data
            })
        },
        search () {
            if (!this.ptime) {
                this.gettest()
            } else if (this.ptime.length > 4) {
                getmonth({ ptime: this.ptime }).then((res) => {
                    this.tableData = res.data
                    this.downdate = this.ptime
                })
            } else {
                getyear({ ptime: this.ptime }).then((res) => {
                    this.tableData = res.data
                    this.downdate = this.ptime
                })
            }
        },
        getSummaries (param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 0) {
                    sums[index] = '合计';
                    return;
                }
                const values = data.map(item => Number(item[column.property]));
                if (index == 4) {
                    if (sums[2] != 0 && sums[1] != 0) {
                        sums[index] = (sums[2] / sums[1]).toFixed(2)
                    } else {
                        sums[index] = ''
                    }

                } else if (index == 7) {
                    if (sums[6] != 0 && sums[2] != 0) {
                        sums[index] = (sums[6] / sums[2] * 100).toFixed(2) + '%'
                    } else {
                        sums[index] = ''
                    }

                } else {
                    if (!values.every(value => isNaN(value))) {
                        sums[index] = values.reduce((prev, curr) => {
                            const value = Number(curr);
                            if (!isNaN(value)) {
                                return prev + curr;
                            } else {
                                return prev;
                            }
                        }, 0);
                        sums[index] += ' ';
                    } else {
                        sums[index] = '';
                    }
                }

            });

            return sums;

        },
    },
    mounted () {
        // this.getdataDictionary()
        this.gettest()
    },
};
</script>
      
<style scoped lang="less">
.el-form {
    padding: 0 10px;
}

.el-date-editor {
    width: 100% !important;
}

.el-form-item {
    padding: 0 10px;
}
/deep/ .el-table__footer-wrapper tbody td.el-table__cell {
    text-align: center;
}
</style>
      